package com.leyou.upload.controller;

import com.leyou.upload.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


@RestController
@RequestMapping
public class UploadController {
    @Autowired
    private UploadService uploadService;

    /*
    - 请求方式：上传肯定是POST
    - 请求路径：/upload/image
    - 请求参数：文件，参数名是file，SpringMVC会封装为一个接口：MultipleFile
    - 返回结果：这里上传与表单分离，文件不是跟随表单一起提交，而是单独上传并得到结果，随后把结果跟随表单提交。因此上传成功后需要返回一个可以访问的文件的url路径
    * */

    /**
     * 本地上传
     * @param file
     * @return
     */
    @PostMapping("/image")
    public ResponseEntity<String> upLoadImages(MultipartFile file) {
        String imageUrl = uploadService.localUploadImage(file);
        return ResponseEntity.ok(imageUrl);


    }

    @GetMapping("/signature")
    public ResponseEntity<Map<String,Object>>getAliSignature(){
        Map<String,Object>resulrt=uploadService.getAliSignature();
        return ResponseEntity.ok(resulrt);
    }

}
